﻿Imports ArfaUI
Imports System.Windows.Forms
Public Class MasterItemPage
    Inherits PageTemplate
    Private dtgroup As DataTable
    Private dtitem As DataTable
    Private Sub getgroup()
        dtgroup = New DataTable
        If Utils.executeSP("proc_itemgroup", New Object() {"select", Convert.DBNull, Convert.DBNull}, dtgroup) Then

        End If
    End Sub
    Private Sub getgroupID(ByVal groupid As Integer)
        dtgroup = New DataTable
        If Utils.executeSP("proc_itemgroup", New Object() {"selectid", groupid, Convert.DBNull}, dtgroup) Then

        End If
    End Sub
    Private Sub getitem(ByVal groupname As String)
        dtitem = New DataTable
        Dim param As New List(Of Object)
        param.Clear()
        param.Add("selectgroup")
        param.Add(Convert.DBNull)
        param.Add(Convert.DBNull)
        param.Add(groupname)
        param.Add(Convert.DBNull)
        param.Add(Convert.DBNull)
        param.Add(Convert.DBNull)
        param.Add(Convert.DBNull)
        param.Add(Convert.DBNull)
        param.Add(Convert.DBNull)
        param.Add(Convert.DBNull)
        param.Add(Convert.DBNull)
        param.Add(Convert.DBNull)

        If Utils.executeSP("proc_item", param.ToArray, dtitem) Then

        End If
    End Sub
    Private Sub createtree()
        getgroup()
        If dtgroup IsNot Nothing Then
            If dtgroup.Rows.Count > 0 Then
                For Each drg As DataRow In dtgroup.Rows
                    'TreeMenu.Nodes.Add(drg("groupitem"), drg("namagroupitem"))
                    Dim node As New TreeNode
                    node.Text = drg("namagroupitem")
                    node.Tag = "G" & drg("groupitem")
                    node.Name = "G" & drg("groupitem")
                    TreeMenu.Nodes.Add(node)
                    getitem(drg("groupitem"))
                    If dtitem IsNot Nothing Then
                        If dtitem.Rows.Count > 0 Then
                            Dim nodes() As TreeNode
                            nodes = TreeMenu.Nodes.Find("G" & drg("groupitem"), True)
                            If nodes.Count() = 1 Then
                                For i As Integer = 0 To dtitem.Rows.Count - 1
                                    Dim inode As New TreeNode
                                    inode.Text = dtitem(i)("namaitem")
                                    inode.Tag = dtitem(i)("iditem")
                                    inode.Name = dtitem(i)("iditem")
                                    nodes(0).Nodes.Add(inode)
                                Next
                            End If
                            
                        End If
                    End If
                Next
            End If
        End If
    End Sub
    Private Sub SETTABcon(ByVal tbc As TabControl)
        Dim i As Integer
        If tbc.TabPages.Count > 0 Then
            For i = 0 To tbc.TabPages.Count - 1
                tabvalue.TabPages.Add(TabControl1.TabPages.Item(0))
            Next
        End If
    End Sub
    Private Sub MasterItemPage_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'tabvalue = New TabControl
        'tabvalue = TabControl1
        'PanelDetail.Controls.Clear()
        'SETTAB(TabControl1)
        TabControl1.Visible = False
        TreeMenu.Nodes.Clear()
        createtree()
    End Sub

    Private Sub MbGlassButton4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MbGlassButton4.Click
        Dim DialogItem As New DialogItem
        DialogItem.execValue = "group"
        DialogItem.ShowDialog()
        TreeMenu.Nodes.Clear()
        createtree()
    End Sub

    Private Sub MbGlassButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MbGlassButton1.Click
        Dim DialogItem As New DialogItem
        DialogItem.execValue = "item"
        DialogItem.ShowDialog()
        TreeMenu.Nodes.Clear()
        createtree()
    End Sub
    Sub searchitem(ByVal finder As String)
        'Dim dtitem As New DataTable
        Dim obj() As Object

        obj = New Object() {"*", "vitem", "iditem", finder}

        If Utils.executeSP("proc_searchall", obj, dtitem) Then

        End If
    End Sub
    Dim tabvalue As New TabControl
    Private Sub TreeMenu_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeMenu.AfterSelect

        'PanelDetail.Controls.Clear()
        'Dim tabdetail As TabControl
        'tabdetail = TabControl1
        
        TabControl1.Visible = True

        'PanelDetail.Controls.Add(tabdetail)

        If e.Node.Tag.ToString.StartsWith("G") Then
            SETTABcon(TabControl1)
            If tabvalue.TabPages.Item(0).Text = "GroupItem" Then
                TabControl1.TabPages.Insert(0, tabvalue.TabPages.Item(0))
            Else
                TabControl1.TabPages.Insert(0, tabvalue.TabPages.Item(1))
            End If

            'tabdetail.TabPages.RemoveAt(1)
            getgroupID(CInt(e.Node.Tag.ToString.Replace("G", "")))
            If dtgroup IsNot Nothing Then
                If dtgroup.Rows.Count > 0 Then
                    For Each ctl As Control In TabControl1.TabPages(0).Controls
                        If TypeOf ctl Is TextBox Then
                            ctl.Text = dtgroup(0)(ctl.Tag)

                        End If
                    Next

                End If
            End If
        Else
            SETTABcon(TabControl1)
            If tabvalue.TabPages.Item(0).Text = "Item" Then
                TabControl1.TabPages.Insert(0, tabvalue.TabPages.Item(0))
            Else
                TabControl1.TabPages.Insert(0, tabvalue.TabPages.Item(1))
            End If
            searchitem(CInt(e.Node.Tag.ToString))
            'tabdetail.TabPages.RemoveAt(0)
            If dtitem IsNot Nothing Then
                If dtitem.Rows.Count > 0 Then
                    For Each ctl As Control In TabControl1.TabPages(0).Controls
                        If TypeOf ctl Is TextBox Then
                            ctl.Text = IIf(IsDBNull(dtitem(0)(ctl.Name.Replace("v", ""))), "", dtitem(0)(ctl.Name.Replace("v", "")))
                            Dim txt As TextBox
                            txt = CType(ctl, TextBox)
                            txt.ReadOnly = True
                        ElseIf TypeOf ctl Is CheckBox Then
                            Dim ct As CheckBox
                            ct = CType(ctl, CheckBox)
                            ct.Checked = dtitem(0)(ctl.Name.Replace("v", ""))
                            ct.Enabled = False
                        End If
                    Next
                End If
            End If
        End If
    End Sub

    Private Sub MasterItemPage_VisibleChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.VisibleChanged
        TabControl1.Visible = False
        TreeMenu.Nodes.Clear()
        createtree()
    End Sub

    Private Sub LinkExpand_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkExpand.LinkClicked
        TreeMenu.ExpandAll()
    End Sub

    Private Sub LinkCollapse_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkCollapse.LinkClicked
        TreeMenu.CollapseAll()
    End Sub
End Class
